package io.task.cc.message.socket.spi.client;

import io.task.cc.message.socket.ChannelContext;
import io.task.cc.message.socket.Config;
import io.task.cc.message.socket.Node;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.AsynchronousSocketChannel;
import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
@Setter
@Getter
public class ClientChannelContext extends ChannelContext {

    private String bindIp;

    private Integer bindPort;

    private ReconnRunnable reconnRunnable;

    /**
     * 连续重连次数，连接成功后，此值会被重置0
     */
    private AtomicInteger reconnCount = new AtomicInteger();


    public ClientChannelContext(Config config, AsynchronousSocketChannel asynchronousSocketChannel) {
        super(config, asynchronousSocketChannel);
    }


    @Override
    public Node createClientNode(AsynchronousSocketChannel asynchronousSocketChannel) throws IOException {

        InetSocketAddress inetSocketAddress = (InetSocketAddress) asynchronousSocketChannel.getLocalAddress();
        Node clientNode = new Node(inetSocketAddress.getHostString(), inetSocketAddress.getPort());

        return clientNode;
    }


    @Override
    public boolean isServer() {
        return false;
    }


}
